var app = getApp();
Page({
    data:{
        page : {
            isend : false,
            doing : false,
            doo : 'ctyagent',
            pdata : {
                op : 'downagent',
                page : 1,
                status : 0,
                areaid : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        posterurl : false,
        isshowp : false,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        self.setData({
            options : options,
            'page.pdata.areaid' : options.areaid ? options.areaid : 0
        });
        app.com.auth(function(user){
            app.com.getPage(self,self.data.page,0,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   params : res.data.data.params,
                   userinfo : data.userinfo,
                   sharedata : data.sharedata,
                },function(){
                    self.setData({
                        inited : true,
                    });
                });

            },false,function(res){

                app.com.alert(res.data.message,function(){
                    if( res.data.errno == 3 ){
                        self.setData({
                            needback : true,
                        }) 
                        wx.navigateTo({
                            url : '/zofui_sales/ctyagent/join'
                        });
                    }
                });
            });
        });

        wx.setNavigationBarTitle({
            title:  '发展代理',
        });
        
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
            'page.pdata.page' : 1,
            'page.isend' : false,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    shareit(e){
        this.setData({
            isshowp :!this.data.isshowp
        })
    },
    copy(){
        var self = this;
        wx.setClipboardData({
            data: self.data.userinfo.agentcode,
            success : function (res) {
                
            }
        })
    },
    cposter(){
        var self = this;
        app.com.http('poster','POST',{op:'ctyagent',path:self.data.sharedata.path,type:1},0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                
                self.setData({
                    posterurl : res.data.data.url,
                    isshowp : true,
                })
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    saveposter : function(){
        var self = this;

        if( !self.data.posterurl ) {
            app.com.alert('无海报图片');
            return false;
        }
        if( self.data.issave ) return false;
        self.data.issave = true;

        wx.downloadFile({
            url: self.data.poster,
            success : function(res) {
                if (res.statusCode === 200) {
                    wx.saveImageToPhotosAlbum({
                        filePath : res.tempFilePath,
                        success : function(){
                            app.com.alert('已保存到手机相册');
                            self.data.issave = false;
                        }
                    })

                }
            }
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },

})